package dev.loong.boot3;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;


@SpringBootApplication
@MapperScan("dev.loong.boot3.dao")
@EnableCaching
@EnableMongoRepositories("dev.loong.boot3.repository")
public class Boot3Application {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisConnectionFactory connectionFactory;
    @Autowired
    private MessageListener redisMessageListener;
    private ThreadPoolTaskScheduler taskScheduler;

    @Bean
    public ThreadPoolTaskScheduler initTaskScheduler() {
        if (taskScheduler != null) {
            return taskScheduler;
        }
        taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(20);
        return taskScheduler;
    }
    @Bean
    public RedisMessageListenerContainer initRedisContainer() {
        var container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setTaskExecutor(initTaskScheduler());
        var topic = new ChannelTopic("topic1");
        container.addMessageListener(redisMessageListener, topic);
        return container;
    }
    public static void main(String[] args) {
//        var ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
//        try {
//            var redisTemplate = ctx.getBean(RedisTemplate.class);
//            redisTemplate.opsForValue().set("key1", "value 1");
//            redisTemplate.opsForHash().put("hash", "field", "hvalue");
//        } finally {
//            ctx.close();
//        }
        SpringApplication.run(Boot3Application.class, args);

    }

}
